#include <iostream>


#ifdef _MAC_OS_
#include <GLUT/glut.h>
#include <OpenGL/gl.h>
#include <OpenGL/glu.h>
#else
#include <GL/glut.h>
#include <GL/gl.h>
#include <GL/glu.h>
#endif

#include "viewer.h"

// window size
#define WIDTH  600
#define HEIGHT 600

using namespace std;

void initGl();
void initGlut(int argc, char **argv);
void reshapeFunc (int w, int h);
void idleFunc();

Viewer v;
//
// End of header
///////////////////////////////////////////////////////////////////////////////


void displayFunc ()
{
  v.display();
}


void 
keyboardFunc (unsigned char key, int x, int y)
{
  switch (key) {
    case 'q':
    case 'Q':
    case 127: // Esc
      exit (EXIT_SUCCESS);
      break;
  }
  
  v.keyPressed(key, x, y);
}

void
mouseFunc(int button, int state, int x, int y){
  v.mousePressed(button, state, x, y);   
}

void
motionFunc(int x, int y){
  v.mouseMoved(x,y);
}

// callback to resize the size of the viewport
void 
reshapeFunc (int w, int h)
{
  v.reshape(w,h);
}


// callback to refresh viewport 
void idleFunc( )
{
    // render the scene
    v.updateScene();
}


// initialize glut framework
void 
initGlut (int argc, char **argv)
{
  glutInit (&argc, argv);
  glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
  glutInitWindowPosition (10, 10);
  glutInitWindowSize (WIDTH, HEIGHT);
  if (glutCreateWindow ("My game") <= 0) {
    std::cerr << "Impossible de creer la fenetre Glut." << endl;;
    exit (EXIT_FAILURE);
  }
  
  // callbacks
  glutDisplayFunc  (displayFunc);
  glutKeyboardFunc (keyboardFunc);
  glutReshapeFunc  (reshapeFunc);
  glutMouseFunc    (mouseFunc);
  glutMotionFunc   (motionFunc); 
  glutIdleFunc     (idleFunc);
}




int
main (int argc, char **argv)
{
  initGlut (argc, argv);
  v.init(WIDTH, HEIGHT);

  glutMainLoop ();
  return (EXIT_SUCCESS);
}


//
// End of File
///////////////////////////////////////////////////////////////////////////////


